#include "reader.h"

namespace SkyID
{

ReaderFile::ReaderFile(const char* fileName)
{
	mFile = fopen(fileName, "rb");
	if (!mFile)
	{
		LOG_MSG("ReaderFile: cannot open file '%s'", fileName);
		return;
	}

	fseek(mFile, 0, SEEK_END);
	mSize = tell();
	fseek(mFile, 0, SEEK_SET);
	setValid(true);
}

ReaderFile::~ReaderFile()
{
	if (mFile)
		fclose(mFile);
}

uint32 ReaderFile::read(void* buffer, uint32 size)
{
	LOG_ASSERT(mFile);
	return (uint32)fread(buffer, 1, (size_t)size, mFile);
}

uint32 ReaderFile::tell() const
{
	LOG_ASSERT(mFile);
	return (uint32)ftell(mFile);
}

bool ReaderFile::reset()
{
	LOG_ASSERT(mFile);
	fseek(mFile, 0, SEEK_SET);
	return true;
}

} // namespace SkyID
